package com.crm.system.controller.orderManager;

import com.alibaba.fastjson.JSONArray;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.common.util.ESUtil;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.customerManager.CustomerOrderBackMoney;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.orderManager.CustomerOrderGiftRelation;
import com.crm.model.entity.orderManager.CustomerProductOrder;
import com.crm.model.entity.orderManager.CustomerProductOrderDetail;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.customerManager.BackMoneyQO;
import com.crm.model.qo.customerManager.OrderBackMoneyQO;
import com.crm.model.qo.orderManager.CustomerProductOrderAddQO;
import com.crm.model.qo.orderManager.CustomerProductOrderQO;
import com.crm.model.qo.orderManager.FinanceProductOrderQO;
import com.crm.model.vo.customerContractManager.CustomerContractDetailVO;
import com.crm.model.vo.customerManager.CustomerOrderBackMoneyVO;
import com.crm.model.vo.orderManager.*;
import com.crm.model.vo.returnMoney.FinanceProductOderInfoVO;
import com.crm.model.vo.sendRoyaltyManager.SendRoyaltyProductVO;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.orderManager.CustomerProductOrderDbService;
import com.crm.system.service.contractManager.CustomerContractService;
import com.crm.system.service.orderManager.CustomerOrderGiftRelationService;
import com.crm.system.service.orderManager.CustomerProductOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ Author ：pym
 * @ Date   ：2021/3/12 10:38
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class CustomerProductOrderController {

    @Value("${esurl}")
    public String ESurl;

    @Autowired
    CustomerProductOrderService customerProductOrderService;

    @Autowired
    CustomerOrderGiftRelationService customerOrderGiftRelationService;

    @Autowired
    CustomerContractService customerContractService;

    @Autowired
    CustomerProductOrderDbService customerProductOrderDbService;

    @Autowired
    private CustomerContractDbService customerContractDbService;

    /**
     * 管理模块-订单管理-订单列表(超级管理员)
     * @Author pym
     * @Date  2021/3/12 15:32
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param customerProductOrderQO -查询条件
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/superOrderList")
    public DataResponse superOrderList(HttpServletRequest request,
                                      @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                      @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                      @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                      @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                      CustomerProductOrderQO customerProductOrderQO//查询条件
    ){
        try{
            DhPageInfo<CustomerProductOrderVO> customerPoolVOList
                    = customerProductOrderService.selectCustomerProductOrderVOList(pageIndex,pageSize,customerProductOrderQO,sortName,sortOrder,"0");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerPoolVOList);
        }catch(Exception e){
            log.error("order superOrderList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 管理模块-订单管理-订单列表（普通管理员）
     * @Author pym
     * @Date  2021/3/15 8:54
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param customerProductOrderQO -查询条件
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/orderList")
    public DataResponse orderList(HttpServletRequest request,
                                  @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                  @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                  @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                  @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                  CustomerProductOrderQO customerProductOrderQO//查询条件
    ){
        try{
            DhPageInfo<CustomerProductOrderVO> customerPoolVOList
                    = customerProductOrderService.selectCustomerProductOrderVOList(pageIndex,pageSize,customerProductOrderQO,sortName,sortOrder,"1");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerPoolVOList);
        }catch(Exception e){
            log.error("order orderList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 用户模块-订单管理-订单列表
     * @Author pym
     * @Date  2021/3/15 8:58
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param customerProductOrderQO -查询条件
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/userOrderList")
    public DataResponse userOrderList(HttpServletRequest request,
                                  @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                  @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                  @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                  @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                  @RequestParam(value="paidBackStatusMultiple",required = false) String paidBackStatusMultiple,
                                  CustomerProductOrderQO customerProductOrderQO//查询条件
    ){
        try{
            List<String> paidBackStatusMultiples = JSONArray.parseArray(paidBackStatusMultiple, String.class);
            customerProductOrderQO.setPaidBackStatusMultiple(paidBackStatusMultiples);
            DhPageInfo<CustomerProductOrderVO> customerPoolVOList
                    = customerProductOrderService.selectCustomerProductOrderVOList(pageIndex,pageSize,customerProductOrderQO,sortName,sortOrder,"2");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerPoolVOList);
        }catch(Exception e){
            log.error("order userOrderList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 订单管理-订单列表-订单详情-产品详情、活动产品列表
     * @Author pym
     * @Date  2021/3/17 10:23
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param productOrderId -产品订单表主键
     * @param productOrderDetailType -订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:查询产品状态为正常的
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectCustomerProductOrderDetailList")
    public DataResponse selectCustomerProductOrderDetailList(HttpServletRequest request,
                                      @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                      @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                      @RequestParam(value = "productOrderId", required = true) String productOrderId,//产品订单表主键
                                      //productOrderDetailType 订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:查询产品状态为正常的
                                      @RequestParam(value = "productOrderDetailType", required = true) String productOrderDetailType
    ){
        try{
            DhPageInfo<CustomerProductOrderDetail> customerProductOrderDetailList
                    = customerProductOrderService.selectCustomerProductOrderDetailList(pageIndex,pageSize,productOrderId,productOrderDetailType);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductOrderDetailList);
        }catch(Exception e){
            log.error("order selectCustomerProductOrderDetailList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 订单管理-订单列表-订单详情-赠品详情列表
     * @Author pym
     * @Date  2021/3/19 9:25
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param productOrderId -产品订单表主键
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectCustomerOrderGiftRelationList")
    public DataResponse selectCustomerOrderGiftRelationList(HttpServletRequest request,
                                                             @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                             @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                             @RequestParam(value = "productOrderId", required = true) String productOrderId//产品订单表主键
    ){
        try{
            DhPageInfo<CustomerOrderGiftRelation> customerOrderGiftRelationList
                    = customerOrderGiftRelationService.selectCustomerOrderGiftRelationList(pageIndex,pageSize,productOrderId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerOrderGiftRelationList);
        }catch(Exception e){
            log.error("order selectCustomerOrderGiftRelationList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务-订单列表
     * @Author chaifuyou
     * @Date  2021/3/19 10:20
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param financeProductOrderQO -查询条件
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/financeOrderList")
    public DataResponse financeOrderList(HttpServletRequest request,
                                         @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                         @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                         @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                         @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                         FinanceProductOrderQO financeProductOrderQO//查询条件
    ){
        try{
            DhPageInfo<FinanceProductOrderVO> customerPoolVOList
                    = customerProductOrderService.selectFinanceOrderVOList(pageIndex,pageSize,financeProductOrderQO,sortName,sortOrder,"0");
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerPoolVOList);
        }catch(Exception e){
            log.error("order financeOrderList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务-订单产品
     * @Author chaifuyou
     * @Date  2021/3/23 08:30
     * @param request
     */
    @GetMapping("/financeOrderInfo")
    public DataResponse financeOrderList(HttpServletRequest request,String productOrderId){

        try{
            FinanceProductOderInfoVO vo =customerProductOrderService.financeProductOderInfo(productOrderId,1);
            if (Objects.nonNull(vo)){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo.getProducts());
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),null);
        }catch(Exception e){
            log.error("order financeOrderInfo error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务-订单产品、活動
     * @Author chaifuyou
     * @Date  2021/3/23 08:30
     * @param request
     */
    @GetMapping("/financeOrderProductAndActivity")
    public DataResponse financeOrderProductAndActivity(HttpServletRequest request,
                                                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
                                                       @RequestParam(value = "pageSize", required = false) Integer pageSize,//分页大小
                                                       @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                       @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                       String productOrderId){

        try{

            DhPageInfo<SendRoyaltyProductVO> products =customerProductOrderService.financeOrderProductAndActivity(productOrderId,pageIndex,pageSize,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),products);
        }catch(Exception e){
            log.error("order financeOrderProductAndActivity error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 订单管理-订单列表-订单详情-活动产品列表-活动下的产品
     * @Author pym
     * @Date  2021/3/26 9:27
     * @param productOrderId -订单ID
     * @param orderRelationId -活动产品ID
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectCustomerOrderActivityRelationById")
    public DataResponse selectCustomerOrderActivityRelationById(@RequestParam(value="productOrderId")String productOrderId,
                                                                @RequestParam(value="orderRelationId")String orderRelationId
    ){
        try{
            List<HashMap> orderActivityRelationProductNames = customerProductOrderService.selectOrderActivityRelationProductNames(productOrderId,orderRelationId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),orderActivityRelationProductNames);
        }catch(Exception e){
            log.error("order selectCustomerOrderActivityRelationById error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 申请退款(业务员端)
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/orderRefund")
    public DataResponse orderRefund(@RequestBody CustomerOrderBackMoney customerOrderBackMoney
    ){
        try{
            String result = customerProductOrderService.orderRefund(customerOrderBackMoney);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
        } catch (Exception e) {
            log.error("order orderRefund error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 客服-退款列表
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/customerOrderBackMoneyList")
    public DataResponse customerOrderBackMoneyList(HttpServletRequest request,
                                                   @RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
                                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,//分页大小
                                                   @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                   @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                   OrderBackMoneyQO orderBackMoneyQO
    ){
        try{
            DhPageInfo<CustomerOrderBackMoneyVO> list = customerProductOrderService.customerOrderBackMoneyList(pageIndex,pageSize,sortName,sortOrder,orderBackMoneyQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("order customerOrderBackMoneyList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务-退款详情
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/customerOrderBackMoneyDetail")
    public DataResponse customerOrderBackMoneyDetail(HttpServletRequest request,
                                                     @RequestParam(value="orderBackMoneyId",required = false) String orderBackMoneyId,  //退款表主键
                                                     @RequestParam(value="productOrderId",required = false) String productOrderId //订单id
    ){
        try{
            Map<String,Object> map = customerProductOrderService.customerOrderBackMoneyDetail(orderBackMoneyId,productOrderId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
        }catch(Exception e){
            log.error("order customerOrderBackMoneyDetail error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 退款(财务端)
     * @Author bj
     * @Date  2021/7/27 9:27
     * @param backMoneyQO -退款产品信息
     */
    @PostMapping("/orderRefundFinance")
    public DataResponse orderRefundFinance(@RequestBody BackMoneyQO backMoneyQO){
        try{
            String result = customerProductOrderService.orderRefundFinance(backMoneyQO);
            CustomerProductOrder customerOrder = customerProductOrderDbService.selectProductOrderById(backMoneyQO.getProductOrderId());
            ESUtil.syncConctract(customerOrder.getContractId(),ESurl);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
        } catch (Exception e) {
            log.error("order orderRefundFinance error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 退码
     */

    @PostMapping("/backProduct")
    public DataResponse backProduct(@RequestParam(value="orderId",required = false) String orderId,
                                    @RequestParam(value="finishOrderProductNum",required = false)  String finishOrderProductNum){
        try{
            Integer result = customerProductOrderService.backProduct(orderId,finishOrderProductNum);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
        } catch (Exception e) {
            log.error("order backProduct error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 管理端-判断是否是总部财务
     * @return
     */
    @GetMapping("/isSuperCaiwu")
    public DataResponse isSuperCaiwu(){
        try{
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
            Boolean rs = false;
            if("3".equals(userAccount.getDeptId())){
                rs = true;
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),rs);
        }catch(Exception e){
            log.error("returnMoney isSuperAdmin error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 管理端-判断是否是
     * @return
     */
    @GetMapping("/isHouxinxue")
    public DataResponse isHouxinxue(){
        try{
            UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
            Boolean rs = false;
            if("4fc840c33d1d4689ae7672e1d38b29f5".equals(userAccount.getAccountId())){
                rs = true;
            }
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),rs);
        }catch(Exception e){
            log.error("returnMoney isHouxinxue error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 订单详情-退款信息
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/orderBackMoneyList")
    public DataResponse orderBackMoneyList(HttpServletRequest request,
                                                   @RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
                                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,//分页大小
                                                   @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                   @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                   @RequestParam(value="productOrderId",required = false) String productOrderId  //订单id
    ){
        try{
            DhPageInfo<CustomerOrderBackMoneyVO> list = customerProductOrderService.orderBackMoneyList(pageIndex,pageSize,sortName,sortOrder,productOrderId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("order orderBackMoneyList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 余额申请退款(业务员端)
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/balanceRefund")
    public DataResponse balanceRefund(@RequestBody CustomerOrderBackMoney customerOrderBackMoney
    ){
        try{
            String result = customerProductOrderService.balanceRefund(customerOrderBackMoney);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
        } catch (Exception e) {
            log.error("order balanceRefund error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 财务-退余额款详情
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/balanceBackMoneyDetail")
    public DataResponse balanceBackMoneyDetail(HttpServletRequest request,
                                                     @RequestParam(value="orderBackMoneyId",required = false) String orderBackMoneyId  //退款表主键
    ){
        try{
            Map<String,Object> map = customerProductOrderService.balanceBackMoneyDetail(orderBackMoneyId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
        }catch(Exception e){
            log.error("order customerOrderBackMoneyDetail error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 余额退款(财务端)
     * @Author bj
     * @Date  2021/7/27 9:27
     * @param backMoneyQO -退款产品信息
     */
    @PostMapping("/balanceRefundFinance")
    public DataResponse balanceRefundFinance(@RequestBody BackMoneyQO backMoneyQO){
        try{
            String result = customerProductOrderService.balanceRefundFinance(backMoneyQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),result);
        } catch (Exception e) {
            log.error("order orderRefundFinance error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 线上订单同步
     * @Author pym
     * @Date  2021/7/29 16:59
     * @param customerProductOrderAddQO 订单信息
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/addOnlineOrder")
    public Map<String, String> addOnlineOrder(CustomerProductOrderAddQO customerProductOrderAddQO){
        Map<String, String> map = new HashMap<String, String>();
        try{
            map = customerProductOrderService.addOnlineOrder(customerProductOrderAddQO);
        } catch (Exception e) {
            log.error("order addOnlineOrder error:", e);
            map.put("code", "E000000");
            map.put("message", "保存异常！");
            map.put("orderId", "");
            return map;
        }
        return map;
    }

    /**
     * 用户端-已完成订单退款产品列表
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/finishOrderBackProductList")
    public DataResponse finishOrderBackProductList(HttpServletRequest request,
                                                   @RequestParam(value = "pageIndex", required = false) Integer pageIndex,//页码
                                                   @RequestParam(value = "pageSize", required = false) Integer pageSize,//分页大小
                                                   @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                   @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                   @RequestParam(value="orderId",required = false) String orderId  //订单id
    ){
        try{
            DhPageInfo<FinishOrderProduct> list = customerProductOrderService.finishOrderBackProductList(pageIndex,pageSize,sortName,sortOrder,orderId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
        }catch(Exception e){
            log.error("order customerOrderBackMoneyList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 用户端-已完成订单退款产品列表
     * @Author bj
     * @Date  2021/7/27 9:27
     */
    @PostMapping("/orderProductDetailNumMoney")
    public DataResponse orderProductDetailNumMoney(HttpServletRequest request,
                                                   @RequestParam(value="orderId",required = false) String orderId  //订单id
    ){
        try{
            BigDecimal money = customerProductOrderService.orderProductDetailNumMoney(orderId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),money);
        }catch(Exception e){
            log.error("order orderProductDetailNumMoney error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务首页订单数量统计
     * @return
     *
     * @Author renshaorong
     * @Date 2021/8/25
     * 增加companyId字段，用于总财务的切换功能
     */
    @PostMapping("/financeIndexOrderInfo")
    public DataResponse financeIndexOrderInfo(String companyId){
        try{
            FinanceIndexOrderInfoVO vo = customerProductOrderService.financeIndexOrderInfo(companyId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
        }catch(Exception e){
            log.error("order financeIndexOrderInfo error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务首页各种订单金额统计
     * @param dateType
     * @return
     *
     * @Author renshaorong
     * @Date 2021/8/25
     * 增加companyId字段，用于总财务的切换功能
     */
    @PostMapping("/financeIndexOrderInfoList")
    public DataResponse financeIndexOrderInfoList(String dateType,String companyId){
        try{
            FinanceIndexOrderInfoListVO vo = customerProductOrderService.financeIndexOrderInfoList(dateType,companyId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
        }catch(Exception e){
            log.error("order financeIndexOrderInfoList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 财务首页-订单产品列表
     * @param pageIndex
     * @param pageSize
     * @param dateType
     * @return
     *
     * @Author renshaorong
     * @Date 2021/8/25
     * 增加companyId字段，用于总财务的切换功能
     */
    @PostMapping("/financeIndexOrderProductList")
    public DataResponse financeIndexOrderProductList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                     String dateType,String companyId){
        try{
            DhPageInfo<FinanceIndexOrderProductVO> vo = customerProductOrderService.financeIndexOrderProductList(dateType,companyId,pageIndex,pageSize);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),vo);
        }catch(Exception e){
            log.error("order financeIndexOrderProductList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    @PostMapping("/selectFinanceShiftOrderInfo")
    public DataResponse selectFinanceOrderInfo(String orderNumber){
        try{
            CustomerProductOrderVO order=  customerProductOrderService.selectCustomerProductOrderByNumber(orderNumber);
            FinanceProductOderInfoVO vo = customerProductOrderService.financeProductOderInfo(order.getProductOrderId(),null);
            CustomerContractDetailVO customerContractDetailVO = customerContractService.selectCustomerContractVODetail(vo.getContractId());
            ShiftReturnMoneyOrderInfoVO shiftVO = new ShiftReturnMoneyOrderInfoVO();
            shiftVO.setOrderNumber(order.getOrderNumber());
            shiftVO.setOrderActualTotalAmount(order.getOrderActualTotalAmount());
            shiftVO.setCreatePeopleName(vo.getCreatePeopleName());
            shiftVO.setCreateTm(order.getCreateTm());
            shiftVO.setContractType(customerContractDetailVO.getContractType());
            shiftVO.setContractNumber(customerContractDetailVO.getContractNumber());
            shiftVO.setContractStartDate(customerContractDetailVO.getContractStartDate());
            shiftVO.setContractEndDate(customerContractDetailVO.getContractEndDate());
            shiftVO.setCoDeveloperList(customerContractDetailVO.getCoDeveloperList());
            shiftVO.setContractId(customerContractDetailVO.getContractId());
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),shiftVO);
        }catch(Exception e){
            log.error("order selectFinanceShiftOrderInfo error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 订单管理-订单列表-订单详情-产品详情
     * @Author pym
     * @Date  2021/9/6 14:39
     * @param request
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param productOrderId -产品订单表主键
     * @param productOrderDetailType -订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:兑换类型;5:补款升级
     * @return {@link {com.crm.common.model.DataResponse}}
     */
    @PostMapping("/selectFinishOrderProductList")
    public DataResponse selectFinishOrderProductList(HttpServletRequest request,
                                                     @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                     @RequestParam(value = "productOrderId", required = true) String productOrderId,//产品订单表主键
                                                     //productOrderDetailType 订单明细类型--1:产品类型;2:活动类型;3:赠品活动类型;4:查询产品状态为正常的
                                                     @RequestParam(value = "productOrderDetailType", required = true) String productOrderDetailType
    ){
        try{
            DhPageInfo<FinishOrderProduct> finishOrderProductList
                    = customerProductOrderService.selectOrderDetailFinishOrderProductList(pageIndex,pageSize,productOrderId,productOrderDetailType);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),finishOrderProductList);
        }catch(Exception e){
            log.error("order selectFinishOrderProductList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    @PostMapping("/orderToReturnMoney")
    public DataResponse orderToReturnMoney(){
        customerProductOrderService.orderToReturnMoney();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    @PostMapping("/repairReturnMoney")
    public DataResponse repairReturnMoney(){
        customerProductOrderService.repairReturnMoney();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }


    @PostMapping("/contrastFinish")
    public DataResponse contrastFinish(){
        customerProductOrderService.contrastFinish();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    @RequestMapping("/finance/orderInfo")
    public DataResponse financeOrderInfo(@RequestParam String productOrderId) {
        FinanceProductOderInfoVO vo = customerProductOrderService.financeProductOderInfo(productOrderId, null);
        CustomerContractDetailVO customerContractDetailVO = customerContractService.selectCustomerContractVODetail(vo.getContractId());
        if (Objects.nonNull(customerContractDetailVO)){
            //隐藏客户信息
            customerContractDetailVO.setContactName(null);
            customerContractDetailVO.setContactDetails(null);
            customerContractDetailVO.setCustomerEmail(null);
        }
        Map map = new HashMap();
        map.put("vo", vo);
        map.put("productOrderId", productOrderId);
        map.put("contractVO", customerContractDetailVO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),map);
    }

}
